package com.easylinkin.linkappapi.iaqi.controller;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.common.utils.DateUtil;
import com.easylinkin.linkappapi.iaqi.entity.vo.CountDaysVo;
import com.easylinkin.linkappapi.iaqi.service.IaqiStatisticService;
import com.easylinkin.linkappapi.iaqi.service.impl.IaqiServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * IiaqiStatistic表控制层
 *
 * @author CodeGenerator
 * @date 2022/06/27
 */
@RestController
@RequestMapping("appIaqiStatistic")
public class IaqiStatisticController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private IaqiStatisticService appIaqiStatisticService;
    @Resource
    private IaqiServiceImpl iaqiService;


    @PostMapping("countDaysGroupByLevel")
    @ApiOperation("空气污染根据等级分组统计")
    public RestMessage countDaysGroupByLevel(@RequestBody CountDaysVo countDaysVo) {
        Assert.notNull(countDaysVo, "参数为空");
        Assert.notNull(countDaysVo.getQueryType(), "参数查询类型为空");
        List<CountDaysVo> record = appIaqiStatisticService.countDaysGroupByLevel(countDaysVo);
        return RestBuilders.successBuilder(record).build();
    }

    @PostMapping("getCurrentIaqiValue")
    @ApiOperation("获取当前空气质量")
    public RestMessage getCurrentIaqiValue() {
        Integer value = iaqiService.getCurrentIaqiValue();
        return RestBuilders.successBuilder(value).build();
    }

    /**
     * 手动补数据 待删除
     *
     * @return result
     */
    @PostMapping("manualSupplementData")
    @ApiOperation("测试定时任务-待删除")
    public RestMessage manualSupplementData(@RequestParam("startTime") String startStr, @RequestParam("endTime") String endStr) {
        Date start = DateUtil.getDateFromYYYYMMDDHHmmss(startStr);
        Date end = DateUtil.getDateFromYYYYMMDDHHmmss(endStr);
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        while (startCalendar.getTime().compareTo(end) <= 0) {
            Calendar c = Calendar.getInstance();
            c.setTime(startCalendar.getTime());
            iaqiService.saveIaqiStatistic(c);
            startCalendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return RestBuilders.successBuilder().build();
    }
    /*
     *//**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     *//*
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.appIaqiStatisticService.getOneById(id))).build();
    }

    *//**
     * 新增数据
     *
     * @param appIaqiStatistic 实体对象
     * @return 新增结果
     *//*
    @PostMapping
    @ApiOperation("新增")
    public RestMessage insert(@RequestBody IaqiStatistic appIaqiStatistic) {
        return RestBuilders.successBuilder().success((this.appIaqiStatisticService.saveOne(appIaqiStatistic))).build();
    }

    *//**
     * 修改数据
     *
     * @param appIaqiStatistic 实体对象
     * @return 修改结果
     *//*
    @PutMapping
    @ApiOperation("修改单条")
    public RestMessage update(@RequestBody IaqiStatistic appIaqiStatistic) {
        return RestBuilders.successBuilder().success(this.appIaqiStatisticService.updateOne(appIaqiStatistic)).build();
    }

    *//**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     *//*
    @DeleteMapping
    @ApiOperation("批量删除")
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        return RestBuilders.successBuilder().success(this.appIaqiStatisticService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody IaqiStatistic appIaqiStatistic, HttpServletRequest request, HttpServletResponse response) {
        appIaqiStatisticService.export(appIaqiStatistic, request, response);
    }*/
}

